package live.sidian.database.synchronizer.infrastructure.sql;

import cn.hutool.core.util.StrUtil;

import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @author sidian
 * @date 2021/4/4 14:14
 */
public class SqlCreator {
    public static String dropDatabase(String dbName) {
        return String.format("DROP DATABASE IF EXISTS `%s`", dbName);
    }

    public static String createDatabase(String dbName, String charset) {
        return String.format("CREATE DATABASE IF NOT EXISTS `%s` CHARACTER SET %s",
                dbName, charset);
    }

    public static String modifyDbCharset(String dbName, String charset) {
        return String.format("ALTER DATABASE `%s` CHARACTER SET %s", dbName, charset);
    }

    public static String useDb(String dbName) {
        return String.format("use `%s`", dbName);
    }

    public static String dropTable(String schema, String tableName) {
        return String.format("DROP TABLE IF EXISTS `%s`.`%s`", schema, tableName);
    }

    public static String dropColumn(String schema, String tableName, String columnName) {
        return String.format("ALTER TABLE `%s`.`%s` DROP COLUMN `%s`", schema, tableName, columnName);
    }

    public static String addColumn(String schema, String tableName, String columnName, String columnDefinition, String after) {
        return String.format("ALTER TABLE `%s`.`%s` ADD COLUMN `%s` %s %s",
                schema, tableName, columnName, columnDefinition, StrUtil.isBlank(after) ? "FIRST" : String.format("AFTER `%s`", after)
        );
    }

    public static String modifyColumn(String schema, String tableName, String columnName, String columnDefinition, String after) {
        return String.format("ALTER TABLE `%s`.`%s` MODIFY COLUMN `%s` %s %s",
                schema, tableName, columnName, columnDefinition, StrUtil.isBlank(after) ? "FIRST" : String.format("AFTER `%s`", after)
        );
    }

    public static String dropIndex(String schema, String tableName, String indexName) {
        return String.format("ALTER TABLE `%s`.`%s` DROP INDEX `%s`", schema, tableName, indexName);
    }

    public static String dropPrimaryKey(String schema, String tableName) {
        return String.format("ALTER TABLE `%s`.`%s` DROP PRIMARY KEY", schema, tableName);
    }

    public static String addIndex(String schema, String tableName, String indexName, Collection<String> columns, boolean isUnique) {
        return String.format("ALTER TABLE `%s`.`%s` ADD %s INDEX %s (%s)", schema, tableName, isUnique ? "UNIQUE" : "", indexName, handleNames(columns));
    }

    public static String addPrimaryKey(String schema, String tableName, Collection<String> columns) {
        return String.format("ALTER TABLE `%s`.`%s` ADD PRIMARY KEY (%s)", schema, tableName, handleNames(columns));
    }

    private static String handleNames(Collection<String> names) {
        return names.stream().map(s -> "`" + s + "`").collect(Collectors.joining(","));
    }

    /**
     * 构建列定义
     */
    public static String buildColumnDefinition(
            String dataType, boolean nullable, String extra, String defaultValue, String comment) {
        return String.format("%s %s %s %s %s",
                dataType,
                nullable ? "null" : "not null",
                StrUtil.isBlank(extra) ? "" : extra,
                defaultValue == null ? "" : String.format("DEFAULT '%s'", SqlHelper.escapeStr(defaultValue)),
                comment == null ? "" : String.format("comment '%s'", SqlHelper.escapeStr(comment))
        );
    }
}
